// 首页JavaScript功能

// 页面加载完成后执行
document.addEventListener('DOMContentLoaded', function() {
    loadDataOverview();
});

// 加载数据概览
async function loadDataOverview() {
    try {
        const response = await axios.get('/api/report');
        const data = response.data;
        
        const overviewContainer = document.getElementById('dataOverview');
        overviewContainer.innerHTML = `
            <div class="col-md-3">
                <div class="card data-card">
                    <div class="number">${data.total_texts}</div>
                    <div class="label">总文本数</div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="card data-card">
                    <div class="number">${data.avg_sentiment}</div>
                    <div class="label">平均情感分数</div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="card data-card">
                    <div class="number">${Math.round((data.sentiment_distribution.positive / data.total_texts) * 100)}%</div>
                    <div class="label">正面评价比例</div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="card data-card">
                    <div class="number">${Object.keys(data.sources).length}</div>
                    <div class="label">数据来源</div>
                </div>
            </div>
        `;
    } catch (error) {
        console.error('加载数据概览失败:', error);
    }
}

// 生成快速报告
async function generateQuickReport() {
    try {
        const button = event.target;
        button.disabled = true;
        button.innerHTML = '<span class="loading"></span> 生成中...';
        
        const response = await axios.get('/api/report');
        const data = response.data;
        
        // 创建报告内容
        const reportContent = `
新疆维吾尔自治区博物馆网络文本分析报告
生成时间：${data.generated_at}

数据概览：
- 总文本数：${data.total_texts}
- 平均情感分数：${data.avg_sentiment}
- 正面评价：${data.sentiment_distribution.positive} (${Math.round((data.sentiment_distribution.positive / data.total_texts) * 100)}%)
- 中性评价：${data.sentiment_distribution.neutral} (${Math.round((data.sentiment_distribution.neutral / data.total_texts) * 100)}%)
- 负面评价：${data.sentiment_distribution.negative} (${Math.round((data.sentiment_distribution.negative / data.total_texts) * 100)}%)

数据来源分布：
${Object.entries(data.sources).map(([source, count]) => `- ${source}: ${count}`).join('\n')}

报告生成完成！
        `;
        
        // 创建下载链接
        const blob = new Blob([reportContent], { type: 'text/plain;charset=utf-8' });
        const url = window.URL.createObjectURL(blob);
        const a = document.createElement('a');
        a.href = url;
        a.download = `新疆博物馆文本分析报告_${new Date().toISOString().split('T')[0]}.txt`;
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
        window.URL.revokeObjectURL(url);
        
        button.disabled = false;
        button.innerHTML = '<i class="fas fa-file-pdf"></i> 生成快速报告';
        
    } catch (error) {
        console.error('生成报告失败:', error);
        alert('生成报告失败，请稍后重试');
    }
}
